package org.jeecg.modules.sistersophie.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sistersophie.entity.CourseInfo;
import org.jeecg.modules.sistersophie.entity.CourseStudentInfo;
import org.jeecg.modules.sistersophie.entity.StudentInfo;
import org.jeecg.modules.sistersophie.facade.CourseInfoFacade;
import org.jeecg.modules.sistersophie.facade.CourseStudentFacade;
import org.jeecg.modules.sistersophie.service.ICourseInfoService;
import org.jeecg.modules.sistersophie.service.impl.CourseInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseInfoFacadeService implements CourseInfoFacade {

    @Autowired
    private ICourseInfoService courseInfoService;

    @Autowired
    private CourseStudentFacade courseStudentFacade;


    @Override
    public boolean addCourseInfo(CourseInfo courseInfo) {
        if(ObjectUtils.isEmpty(courseInfo.getTeacherId()) || StringUtils.isEmpty(courseInfo.getTeacherFirstName())
                || StringUtils.isEmpty(courseInfo.getTeacherLastName())){
            throw new RuntimeException("Teacher information cannot be empty");
        }
        if(courseInfo.getCourseType() != null && courseInfo.getCourseType().intValue() == 1){
            courseInfo.setMaxStudent(courseInfo.getCourseSubType());
        }
        List<CourseInfo> courseInfos =  courseInfoService.selectByNameNotIn(courseInfo.getName(),null);
        if(courseInfos != null && courseInfos.size() > 0){
            throw new RuntimeException(courseInfo.getName()  + " already exists, please re-enter");
        }

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        courseInfo.setCreateName(sysUser.getUsername());
        courseInfo.setCreateTime(new Date());
        courseInfo.setUpdateName(sysUser.getUsername());
        courseInfo.setUpdateTime(new Date());
        courseInfo.setIsDelete(0);
        return courseInfoService.save(courseInfo);
    }

    public void deleteCourse(String id){
        //如果当前课程 存在关联的 学生 不允许删除
        CourseInfo courseInfo = courseInfoService.getById(id);
        int num = courseStudentFacade.selectCourseStudentCount(courseInfo.getId());
        if(num > 0){
            throw new RuntimeException(" This course is already associated with students, this operation is not allowed ");
        }
        courseInfo.setIsDelete(1);
        courseInfoService.updateById(courseInfo);
    }

    public int selectTeacherCourseCount(Integer teacherId){
        QueryWrapper<CourseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId).eq("is_delete",0);
        return courseInfoService.count(queryWrapper);
    }
}
